<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <title>条件变量 - 欢迎来到SY的博客</title><meta name="description" content="条件变量"><meta property="og:title" content="条件变量" />
<meta property="og:description" content="条件变量" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://acking-you.gitee.io/posts/%C3%A6%C3%A4%C3%A5%C3%A9/" /><meta property="og:image" content="https://acking-you.gitee.io/logo.png"/><meta property="article:section" content="posts" />
<meta property="article:published_time" content="2022-08-24T00:00:00+00:00" />
<meta property="article:modified_time" content="2022-08-24T00:00:00+00:00" />

<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://acking-you.gitee.io/logo.png"/>

<meta name="twitter:title" content="条件变量"/>
<meta name="twitter:description" content="条件变量"/>
<meta name="application-name" content="FeelIt">
<meta name="apple-mobile-web-app-title" content="FeelIt"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="canonical" href="https://acking-you.gitee.io/posts/%C3%A6%C3%A4%C3%A5%C3%A9/" /><link rel="prev" href="https://acking-you.gitee.io/posts/%C3%A4%C2%BA%C3%A6%C3%A9/" /><link rel="next" href="https://acking-you.gitee.io/posts/%C3%A5%C3%A7-%C3%A5%C3%A5%C2%BA%C3%A5%C2%BA/" /><link rel="stylesheet" href="/css/page.min.css"><link rel="stylesheet" href="/css/home.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "条件变量",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/acking-you.gitee.io\/posts\/%C3%A6%C3%A4%C3%A5%C3%A9\/"
        },"genre": "posts","keywords": "条件变量","wordcount":  1935 ,
        "url": "https:\/\/acking-you.gitee.io\/posts\/%C3%A6%C3%A4%C3%A5%C3%A9\/","datePublished": "2022-08-24T00:00:00+00:00","dateModified": "2022-08-24T00:00:00+00:00","publisher": {
            "@type": "Organization",
            "name": "作者"},"author": {
                "@type": "Person",
                "name": "作者"
            },"description": "条件变量"
    }
    </script></head><body data-header-desktop="auto" data-header-mobile="auto"><script>(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="欢迎来到SY的博客">欢迎来到SY的博客</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 文章 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-desktop">
                        <a href="#" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="#" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="欢迎来到SY的博客">欢迎来到SY的博客</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-mobile">
                        <a href="#" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="#" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </div>
                    <a href="#" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">文章</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><div class="menu-item"><a href="javascript:void(0);" class="theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div></div>
    </div>
</header><div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div><main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single" data-toc="disable"><div class="featured-image"><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://w.wallhaven.cc/full/kw/wallhaven-kwqqkq.jpg"
        data-srcset="https://w.wallhaven.cc/full/kw/wallhaven-kwqqkq.jpg, https://w.wallhaven.cc/full/kw/wallhaven-kwqqkq.jpg 1.5x, https://w.wallhaven.cc/full/kw/wallhaven-kwqqkq.jpg 2x"
        data-sizes="auto"
        alt="https://w.wallhaven.cc/full/kw/wallhaven-kwqqkq.jpg"
        title="条件变量" /></div><div class="single-card" data-image="true"><h2 class="single-title animated flipInX">条件变量</h2><div class="post-meta">
                <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel=" author" class="author"><i class="fas fa-user-circle fa-fw"></i>作者</a></span>&nbsp;<span class="post-category">出版于  <a href="/categories/cpp%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%BC%82%E6%AD%A5%E4%B8%8E%E5%B9%B6%E5%8F%91/"><i class="far fa-folder fa-fw"></i>CPP多线程异步与并发</a></span></div>
                <div class="post-meta-line"><span><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2022-08-24">2022-08-24</time></span>&nbsp;<span><i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 1935 字</span>&nbsp;
                    <span><i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 4 分钟</span>&nbsp;</div>
            </div>
            
            <hr><div class="details toc" id="toc-static"  data-kept="">
                    <div class="details-summary toc-title">
                        <span>目录</span>
                        <span><i class="details-icon fas fa-angle-right"></i></span>
                    </div>
                    <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#1stdcondition_variable">1、std::condition_variable</a></li>
    <li><a href="#11条件变量等待被唤醒">1.1、条件变量：等待被唤醒</a></li>
    <li><a href="#12条件变量等待某一条件为真">1.2、条件变量：等待某一条件为真</a></li>
    <li><a href="#13条件变量多个等待者">1.3、条件变量：多个等待者</a></li>
    <li><a href="#2实现生产者-消费者模式">2、实现生产者-消费者模式</a></li>
    <li><a href="#3封装消息队列类">3、封装消息队列类</a></li>
  </ul>
</nav></div>
                </div><div class="content" id="content"><h2 id="1stdcondition_variable">1、std::condition_variable</h2>
<p><strong>条件变量是 C++11 提供的另外一种用于等待的同步机制，它能阻塞一个或多个线程，直到收到另外一个线程发出的通知或者超时时，才会唤醒当前阻塞的线程。条件变量需要和互斥量配合起来使用。</strong></p>
<h2 id="11条件变量等待被唤醒">1.1、条件变量：等待被唤醒</h2>
<p>std::condition_variable中的wait方法会让当前的线程陷入等待状态，在其他线程中调用notify_one方法则会唤醒那个陷入等待的线程。</p>
<p>示例代码如下：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-CPP" data-lang="CPP"><span class="cp">#include</span><span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;thread&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;mutex&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;chrono&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;condition_variable&gt;</span><span class="cp">
</span><span class="cp"></span><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
	<span class="cm">/*
</span><span class="cm">	条件变量类似于信号量，只有某个事件发生，这个线程才能执行。
</span><span class="cm">	条件变量等待被唤醒
</span><span class="cm">	*/</span>
	<span class="n">std</span><span class="o">::</span><span class="n">condition_variable</span> <span class="n">cv</span><span class="p">;</span>
	<span class="n">std</span><span class="o">::</span><span class="n">mutex</span> <span class="n">mtx</span><span class="p">;</span>
	<span class="n">std</span><span class="o">::</span><span class="kr">thread</span> <span class="n">t</span><span class="p">([</span><span class="o">&amp;</span><span class="p">]()</span> <span class="p">{</span>
		<span class="n">std</span><span class="o">::</span><span class="n">unique_lock</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">mutex</span><span class="o">&gt;</span><span class="n">ulock</span><span class="p">(</span><span class="n">mtx</span><span class="p">);</span>
		<span class="c1">//等待cv上发生的事件,让当前线程陷入等待
</span><span class="c1"></span>		<span class="n">cv</span><span class="p">.</span><span class="n">wait</span><span class="p">(</span><span class="n">ulock</span><span class="p">);</span>
		<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;thread awake...&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
		<span class="p">});</span>
	<span class="n">std</span><span class="o">::</span><span class="n">this_thread</span><span class="o">::</span><span class="n">sleep_for</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">chrono</span><span class="o">::</span><span class="n">milliseconds</span><span class="p">(</span><span class="mi">400</span><span class="p">));</span>
	<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;notifying...&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
	<span class="c1">//唤醒陷入等待的线程
</span><span class="c1"></span>	<span class="n">cv</span><span class="p">.</span><span class="n">notify_one</span><span class="p">();</span>
	<span class="n">t</span><span class="p">.</span><span class="n">join</span><span class="p">();</span>
	<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><h2 id="12条件变量等待某一条件为真">1.2、条件变量：等待某一条件为真</h2>
<p>std::condition_variable中的wait方法中除了传入独占锁(unique_lock)还可以传入一个lambda表达式，lambda返回为true时，才会唤醒线程，否则继续等待。</p>
<p>示例代码如下：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-CPP" data-lang="CPP"><span class="cp">#include</span><span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;thread&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;mutex&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;chrono&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;condition_variable&gt;</span><span class="cp">
</span><span class="cp"></span><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
	<span class="cm">/*
</span><span class="cm">	条件变量等待某一条件为真，才能被唤醒
</span><span class="cm">	*/</span>
	<span class="n">std</span><span class="o">::</span><span class="n">condition_variable</span> <span class="n">cv</span><span class="p">;</span>
	<span class="n">std</span><span class="o">::</span><span class="n">mutex</span> <span class="n">mtx</span><span class="p">;</span>
	<span class="kt">bool</span> <span class="n">ready</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
	<span class="n">std</span><span class="o">::</span><span class="kr">thread</span> <span class="n">t</span><span class="p">([</span><span class="o">&amp;</span><span class="p">]()</span> <span class="p">{</span>
		<span class="n">std</span><span class="o">::</span><span class="n">unique_lock</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">mutex</span><span class="o">&gt;</span><span class="n">grd</span><span class="p">(</span><span class="n">mtx</span><span class="p">);</span>
		<span class="n">cv</span><span class="p">.</span><span class="n">wait</span><span class="p">(</span><span class="n">grd</span><span class="p">,</span> <span class="p">[</span><span class="o">&amp;</span><span class="p">]()</span> <span class="p">{</span><span class="k">return</span> <span class="n">ready</span><span class="p">;</span> <span class="p">});</span>
		<span class="n">grd</span><span class="p">.</span><span class="n">unlock</span><span class="p">();</span>
		<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;thread is awake....&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
		<span class="p">});</span>
	<span class="n">std</span><span class="o">::</span><span class="n">this_thread</span><span class="o">::</span><span class="n">sleep_for</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">chrono</span><span class="o">::</span><span class="n">milliseconds</span><span class="p">(</span><span class="mi">400</span><span class="p">));</span>
	<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;notifying is not ready...&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
	<span class="n">cv</span><span class="p">.</span><span class="n">notify_one</span><span class="p">();</span>
	<span class="n">ready</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
	<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;notifying is ready&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
	<span class="n">cv</span><span class="p">.</span><span class="n">notify_one</span><span class="p">();</span>
	<span class="n">t</span><span class="p">.</span><span class="n">join</span><span class="p">();</span>
	<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><h2 id="13条件变量多个等待者">1.3、条件变量：多个等待者</h2>
<p>std::condition_variable中的notify_one只会唤醒其中一个等待中的线程，而notify_all则会唤醒所有线程。</p>
<p>这就是为什么wait需要传入unique_lock作为参数，因为我们要保证多个线程被唤醒，但只有一个线程执行，其他线程需要等待那个线程解锁后再执行。</p>
<p>示例代码如下：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-CPP" data-lang="CPP"><span class="cp">#include</span><span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;thread&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;mutex&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;chrono&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;condition_variable&gt;</span><span class="cp">
</span><span class="cp"></span><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
	<span class="cm">/*
</span><span class="cm">	条件变量：多个等待者
</span><span class="cm">	notify_one只会唤醒其中的一个等待的线程，而notify_all会唤醒所有等待的线程。
</span><span class="cm">	这也解释了为什么wait需要一个unique_lock作为参数。
</span><span class="cm">	因为可能同一时间出现多个线程（相当于消息队列），确保线程1执行时，其他线程不会执行，
</span><span class="cm">	只有当线程1解锁后，其他线程才能执行
</span><span class="cm">	*/</span>
	<span class="n">std</span><span class="o">::</span><span class="n">condition_variable</span> <span class="n">cv</span><span class="p">;</span>
	<span class="n">std</span><span class="o">::</span><span class="n">mutex</span> <span class="n">mtx</span><span class="p">;</span>
	<span class="n">std</span><span class="o">::</span><span class="kr">thread</span> <span class="n">t1</span><span class="p">([</span><span class="o">&amp;</span><span class="p">]()</span> <span class="p">{</span>
		<span class="n">std</span><span class="o">::</span><span class="n">unique_lock</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">mutex</span><span class="o">&gt;</span><span class="n">grd</span><span class="p">(</span><span class="n">mtx</span><span class="p">);</span>
		<span class="n">cv</span><span class="p">.</span><span class="n">wait</span><span class="p">(</span><span class="n">grd</span><span class="p">);</span>
		<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;t1 is awake...&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
		<span class="p">});</span>
	<span class="n">std</span><span class="o">::</span><span class="kr">thread</span> <span class="n">t2</span><span class="p">([</span><span class="o">&amp;</span><span class="p">]()</span> <span class="p">{</span>
		<span class="n">std</span><span class="o">::</span><span class="n">unique_lock</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">mutex</span><span class="o">&gt;</span><span class="n">grd</span><span class="p">(</span><span class="n">mtx</span><span class="p">);</span>
		<span class="n">cv</span><span class="p">.</span><span class="n">wait</span><span class="p">(</span><span class="n">grd</span><span class="p">);</span>
		<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;t2 is awake...&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
		<span class="p">});</span>
	<span class="n">std</span><span class="o">::</span><span class="kr">thread</span> <span class="n">t3</span><span class="p">([</span><span class="o">&amp;</span><span class="p">]()</span> <span class="p">{</span>
		<span class="n">std</span><span class="o">::</span><span class="n">unique_lock</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">mutex</span><span class="o">&gt;</span><span class="n">grd</span><span class="p">(</span><span class="n">mtx</span><span class="p">);</span>
		<span class="n">cv</span><span class="p">.</span><span class="n">wait</span><span class="p">(</span><span class="n">grd</span><span class="p">);</span>
		<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;t3 is awake...&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
		<span class="p">});</span>
	<span class="n">std</span><span class="o">::</span><span class="n">this_thread</span><span class="o">::</span><span class="n">sleep_for</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">chrono</span><span class="o">::</span><span class="n">milliseconds</span><span class="p">(</span><span class="mi">400</span><span class="p">));</span>
	<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;notifying one&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
	<span class="n">cv</span><span class="p">.</span><span class="n">notify_one</span><span class="p">();</span>
	
	<span class="n">std</span><span class="o">::</span><span class="n">this_thread</span><span class="o">::</span><span class="n">sleep_for</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">chrono</span><span class="o">::</span><span class="n">milliseconds</span><span class="p">(</span><span class="mi">400</span><span class="p">));</span>
	<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;notifying all&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
	<span class="n">cv</span><span class="p">.</span><span class="n">notify_all</span><span class="p">();</span>
	<span class="n">t1</span><span class="p">.</span><span class="n">join</span><span class="p">();</span>
	<span class="n">t2</span><span class="p">.</span><span class="n">join</span><span class="p">();</span>
	<span class="n">t3</span><span class="p">.</span><span class="n">join</span><span class="p">();</span>
	<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><h2 id="2实现生产者-消费者模式">2、实现生产者-消费者模式</h2>
<p>生产者-消费者模式类似于消息队列，生产者：厨师，往foods队列中推送食物，推送后会通知消费者用餐。</p>
<p>消费者：等待foods队列中推送食物，没有食物则陷入等待，直到被通知。</p>
<p>示例代码如下：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-CPP" data-lang="CPP"><span class="cp">#include</span><span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;thread&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;mutex&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;chrono&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;condition_variable&gt;</span><span class="cp">
</span><span class="cp"></span><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
	<span class="cm">/*
</span><span class="cm">	模拟生产者-消费者模式，类似于消息队列。
</span><span class="cm">	*/</span>
	<span class="n">std</span><span class="o">::</span><span class="n">condition_variable</span> <span class="n">cv</span><span class="p">;</span>
	<span class="n">std</span><span class="o">::</span><span class="n">mutex</span> <span class="n">mtx</span><span class="p">;</span>
	<span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="n">foods</span><span class="p">;</span>
	<span class="n">std</span><span class="o">::</span><span class="kr">thread</span> <span class="n">t1</span><span class="p">([</span><span class="o">&amp;</span><span class="p">]()</span> <span class="p">{</span>
		<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
		<span class="p">{</span>
			<span class="n">std</span><span class="o">::</span><span class="n">unique_lock</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">mutex</span><span class="o">&gt;</span><span class="n">grd</span><span class="p">(</span><span class="n">mtx</span><span class="p">);</span>
			<span class="n">cv</span><span class="p">.</span><span class="n">wait</span><span class="p">(</span><span class="n">grd</span><span class="p">,</span> <span class="p">[</span><span class="o">&amp;</span><span class="p">]()</span> <span class="p">{</span>
				<span class="k">return</span> <span class="n">foods</span><span class="p">.</span><span class="n">size</span><span class="p">()</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">;</span>
				<span class="p">});</span>
			<span class="k">auto</span> <span class="n">ret</span> <span class="o">=</span> <span class="n">foods</span><span class="p">.</span><span class="n">back</span><span class="p">();</span>
			<span class="n">foods</span><span class="p">.</span><span class="n">pop_back</span><span class="p">();</span>
			<span class="n">grd</span><span class="p">.</span><span class="n">unlock</span><span class="p">();</span>
			<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;t1 get foods:&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">ret</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
		<span class="p">}</span>
		<span class="p">});</span>
	<span class="n">std</span><span class="o">::</span><span class="kr">thread</span> <span class="n">t2</span><span class="p">([</span><span class="o">&amp;</span><span class="p">]()</span> <span class="p">{</span>
		<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
		<span class="p">{</span>
			<span class="n">std</span><span class="o">::</span><span class="n">unique_lock</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">mutex</span><span class="o">&gt;</span><span class="n">grd</span><span class="p">(</span><span class="n">mtx</span><span class="p">);</span>
			<span class="n">cv</span><span class="p">.</span><span class="n">wait</span><span class="p">(</span><span class="n">grd</span><span class="p">,</span> <span class="p">[</span><span class="o">&amp;</span><span class="p">]()</span> <span class="p">{</span>
				<span class="k">return</span> <span class="n">foods</span><span class="p">.</span><span class="n">size</span><span class="p">()</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">;</span>
				<span class="p">});</span>
			<span class="k">auto</span> <span class="n">ret</span> <span class="o">=</span> <span class="n">foods</span><span class="p">.</span><span class="n">back</span><span class="p">();</span>
			<span class="n">foods</span><span class="p">.</span><span class="n">pop_back</span><span class="p">();</span>
			<span class="n">grd</span><span class="p">.</span><span class="n">unlock</span><span class="p">();</span>
			<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;t2 get foods:&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">ret</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
		<span class="p">}</span>
		<span class="p">});</span>
	<span class="n">foods</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="mi">45</span><span class="p">);</span>
	<span class="n">cv</span><span class="p">.</span><span class="n">notify_one</span><span class="p">();</span>
	<span class="n">foods</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="mi">255</span><span class="p">);</span>
	<span class="n">cv</span><span class="p">.</span><span class="n">notify_one</span><span class="p">();</span>

	<span class="n">foods</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="mi">333</span><span class="p">);</span>
	<span class="n">foods</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="mi">4398</span><span class="p">);</span>
	<span class="n">cv</span><span class="p">.</span><span class="n">notify_all</span><span class="p">();</span>
	<span class="n">t1</span><span class="p">.</span><span class="n">join</span><span class="p">();</span>
	<span class="n">t2</span><span class="p">.</span><span class="n">join</span><span class="p">();</span>
	<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><h2 id="3封装消息队列类">3、封装消息队列类</h2>
<p>将上述代码中的foods容器封装成为一个消息队列类，这样就比之前的代码执行效率更高。</p>
<p>示例代码如下：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-CPP" data-lang="CPP"><span class="cp">#include</span><span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;thread&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;mutex&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;chrono&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;condition_variable&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;vector&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;algorithm&gt;</span><span class="cp">
</span><span class="cp">#include</span><span class="cpf">&lt;iterator&gt;</span><span class="cp">
</span><span class="cp"></span>
<span class="c1">//自定义多线程安全的消息队列容器
</span><span class="c1"></span><span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">T</span><span class="o">&gt;</span>
<span class="k">class</span> <span class="nc">MTQueue</span>
<span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
	<span class="c1">//弹出栈顶元素
</span><span class="c1"></span>	<span class="n">T</span> <span class="n">pop</span><span class="p">()</span>
	<span class="p">{</span>
		<span class="n">std</span><span class="o">::</span><span class="n">unique_lock</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">mutex</span><span class="o">&gt;</span><span class="n">grd</span><span class="p">(</span><span class="n">m_mtx</span><span class="p">);</span>
		<span class="n">m_cv</span><span class="p">.</span><span class="n">wait</span><span class="p">(</span><span class="n">grd</span><span class="p">,</span> <span class="p">[</span><span class="o">&amp;</span><span class="p">]()</span> <span class="p">{</span>
			<span class="k">return</span> <span class="o">!</span><span class="n">m_vec</span><span class="p">.</span><span class="n">empty</span><span class="p">();</span>
			<span class="p">});</span>
		<span class="n">T</span> <span class="n">ret</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">m_vec</span><span class="p">.</span><span class="n">back</span><span class="p">());</span>
		<span class="n">m_vec</span><span class="p">.</span><span class="n">pop_back</span><span class="p">();</span>
		<span class="k">return</span> <span class="n">ret</span><span class="p">;</span>
	<span class="p">}</span>
	<span class="k">auto</span> <span class="nf">pop_hold</span><span class="p">()</span>
	<span class="p">{</span>
		<span class="n">std</span><span class="o">::</span><span class="n">unique_lock</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">mutex</span><span class="o">&gt;</span><span class="n">grd</span><span class="p">(</span><span class="n">m_mtx</span><span class="p">);</span>
		<span class="n">m_cv</span><span class="p">.</span><span class="n">wait</span><span class="p">(</span><span class="n">grd</span><span class="p">,</span> <span class="p">[</span><span class="o">&amp;</span><span class="p">]()</span> <span class="p">{</span>
			<span class="k">return</span> <span class="o">!</span><span class="n">m_vec</span><span class="p">.</span><span class="n">empty</span><span class="p">();</span>
			<span class="p">});</span>
		<span class="n">T</span> <span class="n">ret</span> <span class="o">=</span> <span class="n">m_vec</span><span class="p">.</span><span class="n">back</span><span class="p">();</span>
		<span class="n">m_vec</span><span class="p">.</span><span class="n">pop_back</span><span class="p">();</span>
		<span class="k">return</span> <span class="n">std</span><span class="o">::</span><span class="n">pair</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">ret</span><span class="p">),</span> <span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">grd</span><span class="p">));</span>
	<span class="p">}</span>
	<span class="c1">//推1个数据
</span><span class="c1"></span>	<span class="kt">void</span> <span class="nf">push</span><span class="p">(</span><span class="n">T</span> <span class="n">val</span><span class="p">)</span>
	<span class="p">{</span>
		<span class="n">std</span><span class="o">::</span><span class="n">unique_lock</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">mutex</span><span class="o">&gt;</span><span class="n">grd</span><span class="p">(</span><span class="n">m_mtx</span><span class="p">);</span>
		<span class="n">m_vec</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">val</span><span class="p">));</span>
		<span class="n">m_cv</span><span class="p">.</span><span class="n">notify_one</span><span class="p">();</span>
	<span class="p">}</span>
	<span class="c1">//推多个数据
</span><span class="c1"></span>	<span class="cm">/*
</span><span class="cm">	在 C++ 的 STL 容器中，可以进行任意长度的数据的初始化，
</span><span class="cm">	使用初始化列表也只能进行固定参数的初始化，如果想要做到和 STL 一样有任意长度初始化的能力，
</span><span class="cm">	可以使用 std::initializer_list 这个轻量级的类模板来实现。
</span><span class="cm">	如果要把一个序列（sequence）拷贝到一个容器（container）中去，通常用std::copy算法。
</span><span class="cm">	C++17语法：
</span><span class="cm">	std::back_insert_iterator:构造向尾端插入的插入迭代器，可以通过该迭代器对容器插入元素。
</span><span class="cm">	std::move_iterator:移动迭代器，其可以实现以移动而非复制的方式，将某个区域空间中的元素移动至另一个指定的空间。
</span><span class="cm">	*/</span>
	<span class="kt">void</span> <span class="nf">push_many</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">initializer_list</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="n">vals</span><span class="p">)</span>
	<span class="p">{</span>
		<span class="n">std</span><span class="o">::</span><span class="n">unique_lock</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">mutex</span><span class="o">&gt;</span><span class="n">grd</span><span class="p">(</span><span class="n">m_mtx</span><span class="p">);</span>
		<span class="n">std</span><span class="o">::</span><span class="n">copy</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move_iterator</span><span class="p">(</span><span class="n">vals</span><span class="p">.</span><span class="n">begin</span><span class="p">()),</span>
			<span class="n">std</span><span class="o">::</span><span class="n">move_iterator</span><span class="p">(</span><span class="n">vals</span><span class="p">.</span><span class="n">end</span><span class="p">()),</span>
			<span class="n">std</span><span class="o">::</span><span class="n">back_insert_iterator</span><span class="p">(</span><span class="n">m_vec</span><span class="p">));</span>
		<span class="c1">//std::copy(vals.begin(), vals.end(), m_vec);
</span><span class="c1"></span>		<span class="n">m_cv</span><span class="p">.</span><span class="n">notify_all</span><span class="p">();</span>
	<span class="p">}</span>
<span class="k">private</span><span class="o">:</span>
	<span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="n">m_vec</span><span class="p">;</span>
	<span class="n">std</span><span class="o">::</span><span class="n">mutex</span> <span class="n">m_mtx</span><span class="p">;</span>
	<span class="n">std</span><span class="o">::</span><span class="n">condition_variable</span> <span class="n">m_cv</span><span class="p">;</span>
<span class="p">};</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
	<span class="n">MTQueue</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="n">foods</span><span class="p">;</span>
	<span class="n">std</span><span class="o">::</span><span class="kr">thread</span> <span class="n">t1</span><span class="p">([</span><span class="o">&amp;</span><span class="p">]()</span> <span class="p">{</span>
		<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
		<span class="p">{</span>
			<span class="k">auto</span> <span class="n">food</span> <span class="o">=</span> <span class="n">foods</span><span class="p">.</span><span class="n">pop</span><span class="p">();</span>
			<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;t1 get foods:&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">food</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
		<span class="p">}</span>
		<span class="p">});</span>
	<span class="n">std</span><span class="o">::</span><span class="kr">thread</span> <span class="n">t2</span><span class="p">([</span><span class="o">&amp;</span><span class="p">]()</span> <span class="p">{</span>
		<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
		<span class="p">{</span>
			<span class="k">auto</span> <span class="n">food</span> <span class="o">=</span> <span class="n">foods</span><span class="p">.</span><span class="n">pop</span><span class="p">();</span>
			<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;t2 get foods:&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">food</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
		<span class="p">}</span>
		<span class="p">});</span>
	<span class="n">foods</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="mi">44</span><span class="p">);</span>
	<span class="n">foods</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="mi">523</span><span class="p">);</span>
	<span class="n">foods</span><span class="p">.</span><span class="n">push_many</span><span class="p">({</span><span class="mi">4399</span><span class="p">,</span><span class="mi">6666</span><span class="p">});</span>
	<span class="n">t1</span><span class="p">.</span><span class="n">join</span><span class="p">();</span>
	<span class="n">t2</span><span class="p">.</span><span class="n">join</span><span class="p">();</span>
	<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><p>注意：</p>
<p><strong>上述代码中的move_iterator(移动迭代器)和back_insert_iterator(尾端插入迭代器)是C++17新特性，</strong></p>
<p><strong>而vs2019默认支持C++11，所以在项目属性中修改以下语言的特性即可。</strong></p>
<p>具体用法参考CPP手册：<a href="https://www.apiref.com/cpp-zh/cpp.html" target="_blank" rel="noopener noreffer">CPP中文手册</a></p>
<p>std::condition_varriable小贴士：</p>
<ul>
<li>std::condition_varriable仅仅支持std::unique_lock<a href="std::mutex" rel="">std::mutex</a>作为wait的参数，如果需要其他类型的mutex锁，可用std::condition_varriable_any。</li>
<li>它还有wait_for和wait_unitl等方法，分别接受chrono时间段和时间点作为参数。</li>
</ul>
</div><div class="post-footer" id="post-footer">
    <div class="post-info"><div class="post-info-tag"><span><a href="/tags/%E6%9D%A1%E4%BB%B6%E5%8F%98%E9%87%8F/">条件变量</a>
                </span></div><div class="post-info-line"><div class="post-info-mod">
                <span>更新于 2022-08-24</span>
            </div><div class="post-info-mod"></div>
        </div><div class="post-info-share">
            <span><a href="javascript:void(0);" title="分享到 Twitter" data-sharer="twitter" data-url="https://acking-you.gitee.io/posts/%C3%A6%C3%A4%C3%A5%C3%A9/" data-title="条件变量" data-hashtags="条件变量"><i class="fab fa-twitter fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Facebook" data-sharer="facebook" data-url="https://acking-you.gitee.io/posts/%C3%A6%C3%A4%C3%A5%C3%A9/" data-hashtag="条件变量"><i class="fab fa-facebook-square fa-fw"></i></a><a href="javascript:void(0);" title="分享到 WhatsApp" data-sharer="whatsapp" data-url="https://acking-you.gitee.io/posts/%C3%A6%C3%A4%C3%A5%C3%A9/" data-title="条件变量" data-web><i class="fab fa-whatsapp fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Line" data-sharer="line" data-url="https://acking-you.gitee.io/posts/%C3%A6%C3%A4%C3%A5%C3%A9/" data-title="条件变量"><i class="fab fa-line fa-fw"></i></a><a href="javascript:void(0);" title="分享到 微博" data-sharer="weibo" data-url="https://acking-you.gitee.io/posts/%C3%A6%C3%A4%C3%A5%C3%A9/" data-title="条件变量" data-image="https://w.wallhaven.cc/full/kw/wallhaven-kwqqkq.jpg"><i class="fab fa-weibo fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Myspace" data-sharer="myspace" data-url="https://acking-you.gitee.io/posts/%C3%A6%C3%A4%C3%A5%C3%A9/" data-title="条件变量" data-description="条件变量"><i data-svg-src="/lib/simple-icons/icons/myspace.min.svg"></i></a><a href="javascript:void(0);" title="分享到 Blogger" data-sharer="blogger" data-url="https://acking-you.gitee.io/posts/%C3%A6%C3%A4%C3%A5%C3%A9/" data-title="条件变量" data-description="条件变量"><i class="fab fa-blogger fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Evernote" data-sharer="evernote" data-url="https://acking-you.gitee.io/posts/%C3%A6%C3%A4%C3%A5%C3%A9/" data-title="条件变量"><i class="fab fa-evernote fa-fw"></i></a></span>
        </div></div><div class="post-nav"><a href="/posts/%C3%A4%C2%BA%C3%A6%C3%A9/" class="prev" rel="prev" title="互斥量"><i class="fas fa-angle-left fa-fw"></i>Previous Post</a>
            <a href="/posts/%C3%A5%C3%A7-%C3%A5%C3%A5%C2%BA%C3%A5%C2%BA/" class="next" rel="next" title="像素刻度尺">Next Post<i class="fas fa-angle-right fa-fw"></i></a></div></div>
</div></article></div>
            </main>
            <footer class="footer"><div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.86.0">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/khusika/FeelIt" target="_blank" rel="noopener noreffer" title="FeelIt 1.0.1"><i class="fas fa-hand-holding-heart fa-fw"></i> FeelIt</a>
        </div><div class="footer-line" itemscope itemtype="http://schema.org/CreativeWork"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2022</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/"></a></span></div>
</div>
</footer>
        </div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-chevron-up fa-fw"></i>
            </a></div><link rel="stylesheet" href="/lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="/lib/animate/animate.min.css"><link rel="stylesheet" href="/lib/katex/katex.min.css"><link rel="stylesheet" href="/lib/katex/copy-tex.min.css"><script src="/lib/autocomplete/autocomplete.min.js"></script><script src="/lib/lunr/lunr.min.js"></script><script src="/lib/lunr/lunr.stemmer.support.min.js"></script><script src="/lib/lunr/lunr.zh.min.js"></script><script src="/lib/lazysizes/lazysizes.min.js"></script><script src="/lib/clipboard/clipboard.min.js"></script><script src="/lib/sharer/sharer.min.js"></script><script src="/lib/katex/katex.min.js"></script><script src="/lib/katex/auto-render.min.js"></script><script src="/lib/katex/copy-tex.min.js"></script><script src="/lib/katex/mhchem.min.js"></script><script>window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":200},"comment":{},"math":{"delimiters":[{"display":true,"left":"$$","right":"$$"},{"display":true,"left":"\\[","right":"\\]"},{"display":false,"left":"$","right":"$"},{"display":false,"left":"\\(","right":"\\)"}],"strict":false},"search":{"highlightTag":"em","lunrIndexURL":"/index.json","lunrLanguageCode":"zh","lunrSegmentitURL":"/lib/lunr/lunr.segmentit.js","maxResultLength":100,"noResultsFound":"没有找到结果","snippetLength":50,"type":"lunr"}};</script><script src="/js/theme.min.js"></script></body></html>
